package com.gimi.cloud.bbp.manage.consumer.service.page.impl;

import com.alibaba.fastjson.JSON;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.page.PageConfigDTO;
import com.gimi.cloud.bbp.manage.consumer.feign.page.PageConfigFeignService;
import com.gimi.cloud.bbp.manage.consumer.service.page.PageConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class PageConfigServiceImpl implements PageConfigService{
	
	private PageConfigFeignService pageConfigFeignService;

	@Autowired
	public PageConfigServiceImpl(PageConfigFeignService pageConfigFeignService) {
		this.pageConfigFeignService = pageConfigFeignService;
	}

	@Override
	public Response getList(String requestBody, HttpServletRequest request, HttpServletResponse response) {
		PageConfigDTO dto = JSON.toJavaObject(JSON.parseObject(requestBody), PageConfigDTO.class);
		return pageConfigFeignService.getList(dto);
	}
	
	@Override
	public Response delInfo(String requestBody, HttpServletRequest request, HttpServletResponse response) {
		PageConfigDTO dto = JSON.toJavaObject(JSON.parseObject(requestBody), PageConfigDTO.class);
		if (dto.getIds() == null) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		return pageConfigFeignService.delInfo(dto);
	}
	
	@Override
	public Response saveInfo(String requestBody, HttpServletRequest request, HttpServletResponse response) {
		PageConfigDTO dto = JSON.toJavaObject(JSON.parseObject(requestBody), PageConfigDTO.class);
		if (dto.getType() == null) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		return pageConfigFeignService.saveInfo(dto);
	}

	@Override
	public Response getInfoById(String requestData, HttpServletRequest request, HttpServletResponse response) {
		PageConfigDTO dto = JSON.toJavaObject(JSON.parseObject(requestData), PageConfigDTO.class);
		if (dto.getId() == null) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
		return pageConfigFeignService.getInfoById(dto);
	}

}
